package com.zznj.xzjc.widget;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

/***
 * FragMent数据适配
 *
 * @author Administrator
 */

public class FragmentAda extends FragmentStatePagerAdapter {


    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mFragmentTitleList = new ArrayList<>();

    public FragmentAda(@NonNull FragmentManager fm, List<Fragment> fragmentList, List<String> fragmentTitleList) {
        this(fm, fragmentList, fragmentTitleList, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    public FragmentAda(@NonNull FragmentManager fm, List<Fragment> fragmentList, List<String> fragmentTitleList, int behavior) {
        super(fm, behavior);
        if (fragmentList != null) {
            mFragmentList.addAll(fragmentList);
        }
        if (fragmentTitleList != null) {
            mFragmentTitleList.addAll(fragmentTitleList);
        }
    }

    public FragmentAda(@NonNull FragmentManager fm, Fragment[] fragmentList, String[] fragmentTitleList) {
        this(fm, fragmentList, fragmentTitleList, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    public FragmentAda(@NonNull FragmentManager fm, Fragment[] fragmentList, String[] fragmentTitleList, int behavior) {
        super(fm, behavior);
        if (fragmentList != null) {
            for (int i = 0; i < fragmentList.length; i++) {
                mFragmentList.add(fragmentList[i]);
            }
        }
        if (fragmentTitleList != null) {
            for (int i = 0; i < fragmentTitleList.length; i++) {
                mFragmentTitleList.add(fragmentTitleList[i]);
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        if (mFragmentList != null && !mFragmentList.isEmpty()) {
            return mFragmentList.get(position);
        } else {
            return null;
        }
    }

    @Override
    public int getCount() {
        if (mFragmentList != null && !mFragmentList.isEmpty()) {
            return mFragmentList.size();
        } else {
            return 0;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (mFragmentTitleList != null && !mFragmentTitleList.isEmpty() && position < mFragmentTitleList.size()) {
            return mFragmentTitleList.get(position);
        } else {
            return null;
        }
    }
}
